package com.ruoyi.merchant.msg.Model;


import com.ruoyi.merchant.msg.requ.AbstractRequMsg;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 1.登录包 (上行协议号：AP00，响应：BP00)
 */
@Slf4j
@Data
public class AP02 extends AbstractRequMsg {
    /**
     * 数据起封包
     */
    public String headCode = "IW";
    /**
     * 协议号
     */
    public final  String funCode = "AP02";
    /**
     * 语言标志
     */
    private String cn;
    /**
     * 基站组数
     */
    private Integer stationSize;

    /**
     * 回复标识，为0时不回复地址，为 1 时，服务器回复地址信息
     */
    private String ret;

    /**
     * 国家码
     */
    private String mcc;
    /**
     * 运营商代码
     */
    private String mnc;
    /**
     * 基站
      */
    private List<Station> stationList;
    /**
     * WIFI组数
     */
    private Integer wifiSize;
    /**
     * wifi 数据
     */
    private List<Wifi> wifiList;
    /**
     * 纬度值
     */
    private String lon;
    /**
     * 经度值
     */
    private String lat;
    /**
     * 转化后地址
     */
    private String address;
    /**
     * 半径
     */
    private long radius;
    /**
     * 类型
     */
    private String type;
    /**
     * 结尾封包
     */
    public String footCode = "#";


     public AP02(){}

    /**
     * IWAP00353456789012345#
     * @param cmdStr
     */
    public AP02(String cmdStr)
    {
        String[] data = cmdStr.split(",");
        if ("IWAP02".equals(data[0]))
        {
            stationList = new ArrayList<>();
            wifiList = new ArrayList<>();
            this.cn = data[1];
            this.ret = data[2];
            this.stationSize = Integer.valueOf(data[3]);
            this.mcc = data[4];
            this.mnc = data[5];
            Station station = new Station();
            for (int i = 6; i < this.stationSize+5; i++) {
                String datum = data[i];
                String[] split = datum.split("\\|");
                station = new Station();
                station.setLac(split[0]);
                station.setCid(split[1]);
                station.setDbm(split[2]);
                stationList.add(station);
            }
            this.wifiSize = Integer.valueOf(data[this.stationSize + 6]);

            Wifi wifi = new Wifi();
            String datum = data[this.stationSize + 7];
            datum = datum.replace("#","");
            String[] split = datum.split("&");
            for (int i = 0; i < split.length; i++) {
                String str = split[i];
                String[] strings = str.split("\\|");
                wifi = new Wifi();
                wifi.setSsid(strings[0]);
                wifi.setMac(strings[1]);
                wifi.setAbs(strings[2]);
                wifiList.add(wifi);
            }

        }
        else {
            log.error("协议格式错误:{}",cmdStr);
        }
    }


    /**
     * LAC|CID|dbm表示一组基站信息，后面数量需和基站组数地应
     * 10：dbm为信号强度多基站信号强度建议从高到低排列后上传，信号强度转正数：150 – abs(dbm)
     */
    @Data
    public class Station{
            private String lac;
            private String cid;
            private String dbm;
    }

    /**
     * ssid|mac地址|信号强度，信号强度转正数：150 – abs(信号强度)
     */
    @Data
    public class Wifi{
        private String ssid;
        private String mac;
        private String abs;
    }

}




